package com.nabi.library.contents.elements.figure
{
	import com.nabi.library.contents.elements.NabiObject;
	import com.nabi.library.contents.elements.Outline;
	import com.nabi.library.contents.elements.segment.CloseSegment;
	import com.nabi.library.contents.elements.segment.LineSegment;
	import com.nabi.library.contents.elements.segment.MoveSegment;
	import com.nabi.library.contents.elements.style.BitmapFill;
	import com.nabi.library.contents.geom.ITransform;
	import com.nabi.library.contents.geom.NativeTransform;
	import com.yzi.core.MediaType;
	
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	
	public class Image extends NabiObject
	{
		private var m_path:String;
		private var m_mediaType:String;
		
		public function Image($width:Number, $height:Number, $matrix:Matrix = null, $bitmapData:BitmapData = null, $path:String = null)
		{		
			SetLink($path);
			
			cacheAsBitmap = true;
			var outline:Outline = new Outline();
			var outlineList:Vector.<Outline> = new Vector.<Outline>();
			
			outline.AddTail(new MoveSegment(Vector.<Number>([0, 0])));
			outline.AddTail(new LineSegment(Vector.<Number>([$width, 0])));
			outline.AddTail(new LineSegment(Vector.<Number>([$width, $height])));
			outline.AddTail(new LineSegment(Vector.<Number>([0, $height])));
			outline.AddTail(new CloseSegment(Vector.<Number>([0, 0])));
			outline.Update();
			outlineList.push(outline);
			super(outlineList);
			
			var matrix:Matrix;
			if($matrix) matrix = $matrix;
			else matrix = new Matrix();
			
			var transform:ITransform = new NativeTransform(this);
			transform.SetMatrix(matrix);
			
			if($bitmapData) {
				m_style.SetFill(new BitmapFill($bitmapData, matrix));
				Redraw();
			}
		}
		
		public function GetExtension(): String
		{
			var result:String;
			switch(m_mediaType) {
				case MediaType.IMAGE_JPEG:
					result = "jpg";
					break;
				case MediaType.IMAGE_PNG:
					result = "png";
					break;
				case MediaType.IMAGE_GIF:
					result = "gif";
					break;
				default:
					result = "";
					break;
			}
			
			return result;
		}
		
		public function GetMediaType(): String { return m_mediaType; }
		
		public function SetMediaType($mediaType:String): void { m_mediaType = $mediaType; }
		
		public function GetLink(): String { return m_path; }
		
		public function SetLink($path:String): void { m_path = $path; }
	}
}